package com.heima.wemedia.config;

import com.heima.model.common.constants.message.NewsUpOrDownConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 创建文章上下架的配置
 * 交换机 队列 关键字
 */
@Configuration
public class DeclareUpOrDownRabbitConfig {
    //交换机
    @Bean
    public TopicExchange newsUpOrDownExchange(){
        //参数：交换机名字 是否持久化 是否自动删除
        return new TopicExchange(NewsUpOrDownConstants.NEWS_UP_OR_DOWN_EXCHANGE,true,false);
    }
    //文章上下架 队列
    @Bean
    public Queue NewsUpForConfig(){
        //参数：队列名字 队列是否持久化
        return new Queue(NewsUpOrDownConstants.NEWS_UP_FOR_ARTICLE_CONFIG_QUEUE,true);
    }
    @Bean
    public Queue NewsDownForConfig(){
        //参数：队列名字 队列是否持久化
        return new Queue(NewsUpOrDownConstants.NEWS_DOWN_FOR_ARTICLE_CONFIG_QUEUE,true);
    }
    //文章索引库上下架
    @Bean
    public Queue NewsUpForEs(){
        //参数：队列名字 队列是否持久化
        return new Queue(NewsUpOrDownConstants.NEWS_UP_FOR_ES_QUEUE,true);
    }
    @Bean
    public Queue NewsDownForEs(){
        //参数：队列名字 队列是否持久化
        return new Queue(NewsUpOrDownConstants.NEWS_DOWN_FOR_ES_QUEUE,true);
    }

    /**
     * 队列与交换机绑定
     */
    //文章上架配置与交换机绑定
    @Bean
    public Binding bindingConfigUp(){
        return BindingBuilder.bind(NewsUpForConfig()).to(newsUpOrDownExchange()).with(NewsUpOrDownConstants.NEWS_UP_ROUTE_KEY);
    }
    //文章下架配置与交换机绑定
    @Bean
    public Binding bindingConfigdown(){
        return BindingBuilder.bind(NewsDownForConfig()).to(newsUpOrDownExchange()).with(NewsUpOrDownConstants.NEWS_DOWN_ROUTE_KEY);
    }
    //Es索引库绑定
    @Bean
    public Binding bindingEsUp(){
        return BindingBuilder.bind(NewsUpForEs()).to(newsUpOrDownExchange()).with(NewsUpOrDownConstants.NEWS_UP_ROUTE_KEY);
    }
    //文章下架配置与交换机绑定
    @Bean
    public Binding bindingEsdown(){
        return BindingBuilder.bind(NewsDownForEs()).to(newsUpOrDownExchange()).with(NewsUpOrDownConstants.NEWS_DOWN_ROUTE_KEY);
    }

}
